const mongoose = require("mongoose")

let goodsModel = mongoose.model("goodsModel")



/**
 * ++++++++++++++++++++++++++++++++++李兴乾+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 */

// 获取商品 分页获取 lxq
module.exports.getGoodsByPageSell = async ({ pageSize, currentPage }) => {
    // 多表关联查询
    // 获取当前集合的数据总条数
    const total = (await goodsModel.find({state:"sell" })).length;
    // rows 获取的是分页的商品数据 .populate('shopsId')多表关联
    const rows = await goodsModel
        .find({state:"sell"})
        .populate('shopsId')
        .limit(pageSize - 0)   // limit(number) 用于设置请求数据的条数
        .skip((currentPage - 1) * pageSize);   // 用于设置跳过多少条数据
    return { total, rows }
}


// 搜索（获取部分商品） lxq
module.exports.searchGoodsSell = async ({ searchType, searchValue, currentPage, pageSize }) => {
    // 精确查询
    // return await goodsModel.find({ [searchType]: searchValue });
    // 模糊查询
    // 满足搜索条件的数据总条数
    const total = (await goodsModel.find({ [searchType]: { $regex: searchValue, $options: '$i' },state:"sell" })).length;
    // 搜索到的分页商品数据
    const rows = await goodsModel
        .find({ [searchType]: { $regex: searchValue, $options: '$i' },state:"sell"})
        .populate('shopsId')
        .limit(pageSize - 0)   // limit(number) 用于设置请求数据的条数
        .skip((currentPage - 1) * pageSize);   // 用于设置跳过多少条数据
    console.log(rows);
    return { total, rows };
}

// 下架商品（改变state的状态）
module.exports.deleteGoodsSell = async ({ _id}) => {
    return await goodsModel.updateOne({ _id }, { state:"unsold"});
}
//通过id查找商品详情getGoodsId
module.exports.getGoodsId = async (goods_id) => {
    console.log(goods_id);
    return await goodsModel.find(goods_id);
}
//修改商品updateGoodsSell
module.exports.updateGoodsSell = async ({_id,name,price,images,num,shopsId}) => {
    // console.log(goods_id);
    return await goodsModel.updateOne({_id},{name,price,images,num,shopsId});
}

/**
 * ++++++++++++++++++++++++++++++++++李兴乾+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 */


 /**
  *+++++++++++++++++++++++++++++++++++++++刘磊+++++++++++++++++++++++++++++++++++++++++++++++
  */

// 获取商品 分页获取 
module.exports.getGoods = async () => {
    // 多表关联查询
    return await goodsModel.find().populate('shopId');
}


// 搜索（获取部分商品）
module.exports.searchGoods = async ({ searchType, searchValue, currentPage, pageSize }) => {
    // 精确查询
    // return await goodsModel.find({ [searchType]: searchValue });
    // 模糊查询
    // 满足搜索条件的数据总条数
    const total = (await goodsModel.find({ [searchType]: { $regex: searchValue, $options: '$i' },state:"sell" })).length;
    // 搜索到的分页商品数据
    const rows = await goodsModel
        .find({ [searchType]: { $regex: searchValue, $options: '$i' },state:"sell"})
        .populate('shopsId')
        .limit(pageSize - 0)   // limit(number) 用于设置请求数据的条数
        .skip((currentPage - 1) * pageSize);   // 用于设置跳过多少条数据
    console.log(rows);
    return { total, rows };
}
module.exports.addGoods = async goods => {
    return await goodsModel.create(goods);
}
// 下架商品（改变state的状态）
module.exports.deleteGoods = async ({ _id}) => {
    return await goodsModel.updateOne({ _id }, { state:"unsold"});
}
//通过id查找商品详情getGoodsId
module.exports.getGoodsId = async _id => {
    return await goodsModel.find(_id);
}
//修改商品updateGoodsSell
module.exports.updateGoods = async ({_id,name,price,images,num,shopsId}) => {
    // console.log(goods_id);
    return await goodsModel.updateOne({_id},{name,price,images,num,shopsId});
}

/**
 * ++++++++++++++++++++++++++++++++++刘磊+++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 */
